package com.LFLWZ.controller;

import com.LFLWZ.ServiceImpl.courseImpl;
import com.LFLWZ.beans.Dto.Dto;
import com.LFLWZ.beans.Dto.Page;
import com.LFLWZ.beans.Dto.adminListQuery;
import com.LFLWZ.beans.POJO.ClassroomCourse;
import com.LFLWZ.beans.POJO.Comments;
import com.LFLWZ.beans.Vo.ClassroomCourseVo;
import com.LFLWZ.beans.Vo.replyVo;
import com.LFLWZ.mapper.CommentsMapper;
import com.LFLWZ.utils.DtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("course")
public class courseController {

    @Autowired
    private courseImpl course;
    @Autowired
    private CommentsMapper commentsMapper;


    @PostMapping("listCourse")
    public Dto listCourse(@RequestBody adminListQuery adminListQuery){
        Dto dto=null;
        if (adminListQuery==null){
            dto= DtoUtil.returnError(4001,"参数错误");
            return dto;
        }
        Page page = course.listcourse(adminListQuery);
        dto=DtoUtil.returnSuccessData(page);
        return dto;
    }
    @GetMapping("Tags")
    public Dto Tags(){
        return DtoUtil.returnSuccessData(course.types());
    }

    @GetMapping("filtrate")
    public Dto filtrate(){
        return DtoUtil.returnSuccessData(course.finFiltrates());
    }

    @GetMapping("columns")
    public Dto columns(){
        return DtoUtil.returnSuccessData(course.columns());
    }
    @GetMapping("lecturer")
    public Dto lecturer(){
        return DtoUtil.returnSuccessData(course.lecturer());
    }

    @PostMapping("insertCourse")
    public Dto insertCourse(@RequestBody ClassroomCourseVo classroomCourseVo){
        Dto dto=null;
        if (classroomCourseVo==null){
            dto=DtoUtil.returnError(4001,"参数错误");
            return dto;
        }
        ClassroomCourseVo classroomCourseVo1= course.insert(classroomCourseVo);
        if (classroomCourseVo1!=null){
            dto=DtoUtil.returnSuccessData(classroomCourseVo1);
        }else {
            dto=DtoUtil.returnError(4001,"新增失败");
        }
        return dto;
    }

    @PostMapping("reply")
    public Dto reply(@RequestBody replyVo reply){
        Dto dto=null;
        if (reply==null){
            dto=DtoUtil.returnError(4001,"参数错误");
            return dto;
        }
        Comments comments=new Comments("reply",reply.getCommentNameId(),reply.getCommentId(),reply.getContent());
        int line= commentsMapper.insert(comments);
        if (line>0)
            dto=DtoUtil.returnSuccessData(line);
        else
            dto=DtoUtil.returnError(4002,"添加失败");
        return dto;
    }
}
